package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class ChangePasswordContract {

    public ChangePasswordContract() { }
    public ChangePasswordContract(String oldPasswordMD5, String newPasswordMD5)
    {
        this.OldPasswordMD5 = oldPasswordMD5;
        this.NewPasswordMD5 = newPasswordMD5;
    }

    public String OldPasswordMD5 ="";

    public String NewPasswordMD5 ="";

    /**
     * 消息转byte
     *
     * @return
     */
    public byte[] serialize() throws Exception {

        byte[] bNewPasswordMD5 = NewPasswordMD5.getBytes(StandardCharsets.UTF_8);
        byte[] bOldPasswordMD5 = OldPasswordMD5.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bNewPasswordMD5.length + 4 + bOldPasswordMD5.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bNewPasswordMD5.length);
        body.writeBytes(bNewPasswordMD5);
        body.writeInt(bOldPasswordMD5.length);
        body.writeBytes(bOldPasswordMD5);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }
}
